#include "commentmanagement.h"
#include "ui_commentmanagement.h"

commentManagement::commentManagement(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::commentManagement)
{
    ui->setupUi(this);

    db = QSqlDatabase::database("conn");//链接数据库
    initList();
}

commentManagement::~commentManagement()
{
    delete ui;
}


void commentManagement::on_commentList_itemClicked(QListWidgetItem *item)
{
    commentForm* comment =dynamic_cast<commentForm*>(ui->commentList->itemWidget(item));
    int id = comment->getid();//获取对应评论id
    QSqlQuery query;//创建对象时，系统会自动完成跟数据库的关联
    query.setForwardOnly(true);//减少内存浪费

    QMessageBox box(QMessageBox::Information, "友情提示","选择要对该评论进行的操作");
    box.setStandardButtons(QMessageBox::Ok | QMessageBox::No | QMessageBox::Cancel);
    box.setButtonText(QMessageBox::Ok, QString("切换精选评论状态"));
    box.setButtonText(QMessageBox::No, QString("回复该评论"));
    box.setButtonText(QMessageBox::Cancel, QString("取消"));
    int ret = box.exec();
    if(ret == QMessageBox::Cancel)
    {
        return;
    }else if(ret == QMessageBox::Ok){
        int selected = comment->selectedReverse();
        QString sqlUpdate = QString("update comment set selected = %1 where id = %2").arg(selected).arg(id);

        if(!query.exec(sqlUpdate)){
            qDebug() << "update data error" << db.lastError();
        }else{
            QMessageBox::information(this, "友情提示", "切换精选评论状态成功");
            initList();
        }
    }else{
        QString extra,content,customer;
        QString sqlSelect = QString ("select * from comment where id = %1;").arg(id);

        if(!query.exec(sqlSelect)){//查找关键字
            qDebug() << "search data error" << db.lastError();
            return;
        }else {
            if(query.next()){
                customer = query.value("customer").toString();
                content = query.value("content").toString();
                extra = query.value("extra").toString();//获取extra
            }
        }
        replyComment* reply = new replyComment;
        reply->set(id,customer,content,extra);
        reply->show();
    }
}

void commentManagement::initList()
{
    ui->commentList->clear();
    QSqlQuery query;//创建对象时，系统会自动完成跟数据库的关联
    query.setForwardOnly(true);

    QString sqlSelect = QString ("select * from comment where selected = 1 order by id desc;");
    if(!query.exec(sqlSelect)){//遍历comment表单
        qDebug() << "insert data error" << db.lastError();
    }else {
        while(query.next()){//读取数据库，填写列表
            int id = query.value("id").toInt();
            QString dishes = query.value("dishes").toString();
            QString customer = query.value("customer").toString();
            QString date = query.value("date").toString();
            QString content = query.value("content").toString();
            commentForm* commentItem = new commentForm;
            commentItem->setComment(id,dishes,customer,date,content,1);
            QListWidgetItem* item = new QListWidgetItem;

            item->setSizeHint(QSize(1100,228));
            ui->commentList->addItem(item);
            ui->commentList->setItemWidget(item,commentItem);
        }
    }
    sqlSelect = QString ("select * from comment where selected = 0 order by id desc;");
    if(!query.exec(sqlSelect)){//遍历comment表单
        qDebug() << "insert data error" << db.lastError();
    }else {
        while(query.next()){//读取数据库，填写列表
            int id = query.value("id").toInt();
            QString dishes = query.value("dishes").toString();
            QString customer = query.value("customer").toString();
            QString date = query.value("date").toString();
            QString content = query.value("content").toString();
            commentForm* commentItem = new commentForm;
            commentItem->setComment(id,dishes,customer,date,content,0);
            QListWidgetItem* item = new QListWidgetItem;

            item->setSizeHint(QSize(1100,228));
            ui->commentList->addItem(item);
            ui->commentList->setItemWidget(item,commentItem);
        }
    }
}
